package login.Model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import spUtil.DB;

public class authenticate {
	private int idloginMaster;
	private String loginUsername;
	private String loginPassword;
	private String studentRollno;
	
	public String getStudentRollno() {
		return studentRollno;
	}

	public void setStudentRollno(String studentRollno) {
		this.studentRollno = studentRollno;
	}

	public int getIdloginMaster() {
		return idloginMaster;
	}

	public void setIdloginMaster(int idloginMaster) {
		this.idloginMaster = idloginMaster;
	}

	public String getLoginUsername() {
		return loginUsername;
	}

	public void setLoginUsername(String loginUsername) {
		this.loginUsername = loginUsername;
	}

	public String getLoginPassword() {
		return loginPassword;
	}

	public void setLoginPassword(String loginPassword) {
		this.loginPassword = loginPassword;
	}


	static int flag = 0;

	public static int checkAuthentication(String uname, String pswrd) throws SQLException{
		flag = 0;
		int sid;
		
		Map session = ActionContext.getContext().getSession();
		String sqlquery = "select * from loginmaster where loginUsername = ? and loginPassword = ?";
		Connection con = DB.getConnection();
		PreparedStatement st = con.prepareStatement(sqlquery);
		st.setString(1, uname);
		st.setString(2, pswrd);
		ResultSet rs = st.executeQuery();
		if(rs.next()){
			if(rs.getString(5) != null){  // to check that database has value for last login or not
				session.put("sessionlogintime", rs.getString("logindatetime"));  // storing the last login information into the session
				//st = con.prepareStatement("");
			}
			else{
				session.put("sessionlogintime", null);
			}
			if(rs.getInt(4) == 0){  // checking if the login type is admin by checking the studentID which is set 0 for admin
				flag = 2;
			}
			else{  // if login type is student storing its some informations into session
				sid = rs.getInt(4);
				rs.close();
				st = con.prepareStatement("select * from studentmaster where idstudentMaster = ? ");
				st.setInt(1, sid);
				rs = st.executeQuery();
				if(rs.next()){
					session.put("sessionstudentid", sid);
					session.put("sessionstudentname", rs.getString("studentName"));
					session.put("sessionstudentimage", rs.getString("studentImage"));
					flag = 1;
				}
				else{
					flag = 0;
				}
			}
		}
		else{
			flag = 0;
		}
		rs.close();
		con.close();
		return flag;
	}
	
	public static int addauthentication(String uname, String pswrd, String sid) throws SQLException{
		int idstudent;
		String sqlquery, logintype;
		Connection con;
		PreparedStatement st;
		ResultSet rs;
		
		flag = 0;
		con = DB.getConnection();
		if(sid.equals("0")){
			idstudent = 0;
			logintype = "1";
		}
		else{
			sqlquery = "select idstudentMaster from studentmaster where studentRollno = ? ";
			st = con.prepareStatement(sqlquery);
			st.setString(1, sid);
			rs = st.executeQuery();
			if(rs.next()){
				idstudent = rs.getInt("idstudentMaster");
				logintype = "2";
			}
			else{
				flag = 0;
				return flag;
			}
		}
		sqlquery = "insert into loginmaster (loginUsername, loginPassword, studentID, logintype) values(?, ?, ?, ?)";
		con = DB.getConnection();
		st = con.prepareStatement(sqlquery);
		st.setString(1, uname);
		st.setString(2, pswrd);
		st.setInt(3, idstudent);
		st.setString(4, logintype);
		st.execute();
		flag = 1;
		con.close();
		return flag;
	}
	
	public static ArrayList<authenticate> findall(String selectionModifier) throws SQLException {
		ArrayList<authenticate> selection = new ArrayList<authenticate>();
		ResultSet rs = null;
		String query = "select l.idloginMaster, l.loginUsername, l.loginPassword, s.studentRollno " +
				" from loginmaster l, studentmaster s " + 
				" where l.studentID = s.idstudentMaster " +
				selectionModifier;
		Connection con = DB.getConnection();
		PreparedStatement st = con.prepareStatement(query);
		rs = st.executeQuery();
		try {
			while (rs.next()) {
				authenticate ltemp = new authenticate();
				ltemp.setIdloginMaster(rs.getInt("idloginMaster"));
				ltemp.setLoginUsername(rs.getString("loginUsername"));
				ltemp.setLoginPassword(rs.getString("loginPassword"));
				ltemp.setStudentRollno(rs.getString("studentRollno"));
				selection.add(ltemp);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		con.close();
		return selection;
	}
	
	public static void deleteauthenticate(String loginid) throws SQLException{
		String query = 
				"delete from loginmaster " +
				"where idloginMaster = " + loginid;
		Connection con = DB.getConnection();
		PreparedStatement st = con.prepareStatement(query);
		st.execute();
		con.close();
	}
	
	public static int searchauthenticate(String rollno){
		flag = 0;
		
		return flag;
	}
}
